package simple.cashflow.service.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import simple.cashflow.model.Pegawai;
import simple.cashflow.service.PegawaiService;

@Service("pegawaiService")
public class PegawaiServiceImpl implements PegawaiService{
	
	@Autowired
	private SessionFactory sessionFactory;
	private String hqlCariSemua = "from Pegawai";
	private String hqlCariDariNip = "from Pegawai p where p.nip =:nip";

	@Override
	public void simpanPegawai(Pegawai pegawai) {
		sessionFactory.getCurrentSession().saveOrUpdate(pegawai);
	}

	@Override
	public void hapusPegawai(Pegawai pegawai) {
		sessionFactory.getCurrentSession().delete(pegawai);
	}

	@Override
	public Pegawai cariPegawaiDariNip(String nip) {
		return (Pegawai) sessionFactory.getCurrentSession().createQuery(hqlCariDariNip)
					.setParameter("nip", nip).uniqueResult();
	}

	@Override
	public List<Pegawai> cariSemuaPegawai() {
		return sessionFactory.getCurrentSession().createQuery(hqlCariSemua).list();
	}

}
